package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-07
 */
@RestController
@RequestMapping("/eduservice/eduVideo")
//@CrossOrigin
public class EduVideoController {

    @Autowired
    EduVideoService eduVideoService;

    @Autowired
    VodClient vodClient;

    //添加小节信息
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //删除小节信息并且删除阿里云里面的视频
    @DeleteMapping("/deleteVideo/{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        EduVideo byId = eduVideoService.getById(videoId);
        String id = byId.getVideoSourceid();
        if(!StringUtils.isEmpty(id)){
            vodClient.deleteAliyunVod(id);
        }
        eduVideoService.removeById(videoId);
        return R.ok();
    }

    //回显小节信息
    @GetMapping("/getVideo/{videoId}")
    public  R getVideo(@PathVariable String videoId){
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return R.ok().data("eduVideo",eduVideo);
    }

    //修改对应小节信息
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }




}

